package com.zbbz.aop;

import com.ruoyi.common.core.domain.AjaxResult;
import com.zbbz.util.TokenUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author WangYang
 * @date 2025/02/13/16:13
 */
@Component
@Aspect
public class IsLoginAop {

    @Resource
    private TokenUtil tokenUtil;


    @Pointcut("@annotation(com.zbbz.aop.IsLogin))")
    public void isLogin(){}


    @Around("isLogin()")
    public Object process(ProceedingJoinPoint point) throws Throwable {

        // 获取request对象
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes sra = (ServletRequestAttributes) ra;
        HttpServletRequest request = sra.getRequest();

        Integer userId = tokenUtil.getUserId(request);

        if (userId != null){
//            VrEmployee vrEmployee = vrEmployeeService.selectVrEmployeeById(Long.valueOf(userId));
//            if (vrEmployee != null){
//                TokenUtil.vrEmployeeThreadLocal.set(vrEmployee);
//                try {
//                    Object returnValue = point.proceed(point.getArgs());
//                    return returnValue;
//                } finally {
//                    TokenUtil.vrEmployeeThreadLocal.remove();
//                }
//            }
        }

        return AjaxResult.error("未登录！");
    }

}
